
local vkeypath = GetScriptDir() .. 'overlays128.png';

local dpad = {
x = 0,y=0,w = 128,h = 128,
}

local select = {
x = 0,y=dpad.h,w=64,h=36
}

local start = {
x = 64,y=dpad.h,w=64,h=36
}

local A = {
x = 0,y=start.y+start.h,w=64,h=64
}

local B = {
x = 64,y=A.y,w = 64,h=64
}

local L = {
x=0,y=A.y+A.h,w=64,h=64
}

local R = {
x=64,y=L.y,w=64,h=64
}

local ffi = require("ffi")
local SDL2 = loadso('SDL2');
local rect = ffi.new('SDL_Rect[3]');

function CreateGBAVKey(window)
    local key = {};
    key.image = vkeypath;
    key.texture = window:LoadImageTexture(key.image);
    key.renderer = window.renderer;
    table.insert(key,dpad);
    table.insert(key,select);
    table.insert(key,start);
    
    table.insert(key,A);
    table.insert(key,B);
    table.insert(key,L);
    table.insert(key,R);

    function key:Zoom(z)
        for k,v in ipairs(self)do
            local rect = v.dstRect;
            rect.w = v.w * z;
            rect.h = v.h * z;
        end
    end

    function key:SetPos(index,x,y)
        if(self[index])then
            local rect = self[index].dstRect;
            if(rect)then
                rect.x = x;
                rect.y = y;
            end
        end
    end

    function key:SetSize(index,w,h)
        if(self[index])then
            local rect = self[index].dstRect;
            if(rect)then
                rect.x = w;
                rect.y = h;
            end
        end
    end

    function key:GetViews()
        local views = {};
        for k,v in ipairs(self)do
            local view = {texture = self.texture,renderer = self.renderer};
            view.srcRect = v.srcRect;
            view.dstRect = v.dstRect;
            function view:Render()
                SDL2.SDL_RenderCopy(self.renderer,self.texture,v.srcRect,v.dstRect);
            end
            function view:GetRenderRect()
                return self.dstRect;
            end
            table.insert(views,view);
        end
        return views;
    end

    function key:SetOnTouch(index,callback)
        if(self[index])then
            self[index].OnTouch = callback;
        end
    end

    for k,v in ipairs(key)do
        local rect = ffi.new('SDL_Rect');
        rect.x = v.x;
        rect.y = v.y;
        rect.w = v.w;
        rect.h = v.h;
        v.srcRect = rect;
        rect = ffi.new('SDL_Rect');
        v.dstRect = rect;
    end

    --默认大小和位置
    local w,h = window.width,window.height;
    key:Zoom(1*w/480);
    key:SetPos(1,w/8,h/2);
    key:SetPos(2,w/4,h/3);
    key:SetPos(3,w/2,h/3);

    key:SetPos(4,w/2,h/1.7);
    key:SetPos(5,w/1.4,h/1.7);
    key:SetPos(6,w/2,h/2.3);
    key:SetPos(7,w/1.4,h/2.3);
    --
    key.tmpbuf = ffi.new('int[4]');

    
    function key:Render()
        for k,v in ipairs(self)do
            SDL2.SDL_RenderCopy(self.renderer,self.texture,v.srcRect,v.dstRect);
        end
    end
    function key:Event(event)
        local tfinger = event.tfinger;
        event = event.motion;
        local x,y;

        if(ANDROID)then 
            --安卓平台需要设置这个获得触屏位置
            if(SDL2.SDL_GetMouseState(key.tmpbuf,key.tmpbuf+1)~=0)then 
                x,y = key.tmpbuf[0],key.tmpbuf[1];
            end
        else 
            if(event.type>=0x400 and event.type<0x405)then
                x,y = event.x,event.y;
            elseif(event.type>=0x700 and event.type<0x705)then 
                --触摸屏事件
                x,y = tfinger.x,tfinger.y;
                x = x * window.width;
                y = y * window.height;
            end
        end

        if(x)then 
            local flg = event.state==1 or event.type==0x401;
            if(ANDROID)then
                flg = true;
            end
            --按键时间
            if(flg)then
                
                for k,v in ipairs(self)do
                    local dx,dy,dw,dh = v.dstRect.x,v.dstRect.y,v.dstRect.w,v.dstRect.h;
                    dx = x - dx;
                    dy = y - dy;
                    if(dx>0 and dx<dw and dy>0 and dy<dh)then
                        --触发事件
                        if(v.OnTouch)then
                            v:OnTouch(dx,dy,dw,dh);
                        end
                    end
                end
            end
        end
    end
    return key;
end


